﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    [HotUpdate]
    [Description("手续费分摊到单据体")]
    public class AllocationOfHandlingFees : AbstractDynamicFormPlugIn
    {
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            decimal F_WNEG_Amount = 0;
            decimal sxf = 0;
            if (e.Field.Key.Equals("F_WNEG_Amount"))
            {
                if (this.View.Model.GetValue("F_WNEG_Amount") != null)
                {
                    F_WNEG_Amount = Convert.ToDecimal(this.View.Model.GetValue("F_WNEG_Amount"));
                    // 计算实收金额的合计值
                    var summaryData = 0m;
                    var entity = this.View.BillBusinessInfo.GetEntity("FEntity");
                    var entityRow = this.View.Model.GetEntryRowCount("FEntity");
                    // 获取单据体的数据包
                    var entityObjs = this.View.Model.GetEntityDataObject(entity);
                    if (entityObjs != null)
                    {
                        foreach (var entityObj in entityObjs)
                        {
                            // 遍历单据体数据包对某一列求和
                            summaryData += Convert.ToDecimal(entityObj["F_WNEG_Amount2"]);
                        }
                    }
                    if(entityRow!=0)
                    {
                        for(int i=0;i<entityRow;i++)
                        {
                            sxf=Convert.ToDecimal(this.View.Model.GetValue("F_WNEG_Amount2", i))/Convert.ToDecimal(summaryData)* F_WNEG_Amount;
                            this.View.Model.SetValue("F_WNEG_Amount1", sxf, i);
                        }
                    }
                }
            }
        }
    }
}
